//===================================================================================
// $$
//
//
// Code By: Code Milker V2
// Date On: 2013//
//===================================================================================


namespace Forest.Lion.Data.SqlServer.Repositories
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using Forest.Lion.Model;

    public class DepartmentRepository : BaseRepository, IDepartmentRepository
    {
        public DepartmentRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }
        public void Create(Department department)
        {
            this.GetDbSet<Department>().Add(department);
            this.UnitOfWork.SaveChanges();
        }

        public void Update(Department updatedDepartment) {
            this.GetDbSet<Department>().Attach(updatedDepartment);
            this.SetEntityState(updatedDepartment, updatedDepartment.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public Department GetDepartment(Guid departmentId)
        {
            return this.GetDbSet<Department>()
                .Where(c => c.Id == departmentId)
                .Single();
        }

        

        public void Delete(Guid departmentId)
        {
            Department departmentToDelete = this.GetDepartment(departmentId);
            this.GetDbSet<Department>().Remove(departmentToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IEnumerable<Department> Find()
        {
            return this.GetDbSet<Department>().Take(100)
                .ToList();
        }

        public IEnumerable<Department> GetAll()
        {
            return this.GetDbSet<Department>().Take(100)
                .ToList();
        }

        public IEnumerable<Department> GetDepartmentsForCompany(Guid companyId)
        {
            return this.GetDbSet<Department>().Where(x => x.CompanyId.Value.Equals(companyId)).ToList();
        }
    }
}
